package very.util.scalikejdbc

import scalikejdbc._
import scalikejdbc.interpolation.SQLSyntax
import skinny.Pagination

//TODO: String => SQLSyntax 可能会抛异常
case class PageSearch(builder: Map[String, String => SQLSyntax]) {
  def getWhereAndPagination(where: Map[String, Seq[String]]): (SQLSyntax, Pagination) = {
    (
      builder
        .map {
          case (key, func) => where.get(key).flatMap(v => v.headOption.map(func))
        }
        .foldLeft(sqls"1=1")(_.and(_)),
      PaginationFix(
        where.get("pageSize").flatMap(_.headOption.map(_.toInt)).getOrElse(20),
        where.get("page").flatMap(_.headOption.map(_.toInt)).getOrElse(1)
      )
    )
  }
}
object PageSearch {
  def simple = PageSearch(Map.empty)
}
